<?php
namespace app\index\controller;

use app\common\controller\IndexBaseController;
use app\admin\model\Member as MemberModel;

class LoginController extends IndexBaseController {

	/**
	 * 会员登录
	 */
	public function index() {
		if (request() -> isPost()) {
			//登录
			$mobile = request() -> param("mobile");
			$password = request() -> param("password");
			$where['mobile'] = $mobile;
			$where['password'] = md5_salt($password);
			$result = db("member") -> where($where) -> find();
			if($result){
				if($result['status']==0){
					return $this->toError(404,'该用户已被禁用');
				}
				$member_model = new MemberModel();
				$data['login_time'] = now_time();
				$data['login_ip'] = request()->ip();
				$data['session_id'] = session_id();  //更改登录状态
				$member_model->allowField(true)->save($data,['id'=>$result['id']]);
				session('USER',$result);
				return $this->toSuccess('登录成功！');
			}else{
				return $this->toError(404,'用户名不存在或密码错误');
			}
		} else {
			return $this -> fetch();
		}
	}
	
}
